package dao

import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class SampleIdDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends
  HasDatabaseConfigProvider[JdbcProfile] {


  import utils.MyPostgresProfile.api._

  type MyRow = SampleIdRow
  val table = SampleId

  def insert(row: MyRow) = {
    db.run(table += row)
  }

  def selectAll: Future[Seq[MyRow]] = db.run(table.result)

  def updates(rows: Seq[MyRow]): Future[Unit] = {
    val action = DBIO.sequence(
      rows.map { row =>
        table.insertOrUpdate(row)
      }
    ).transactionally
    db.run(action).map(_ => ())
  }

}
